## extract gtf file that overlapped
## author: Yaping Liu  lyping1986@gmail.com

my $file_name=$ARGV[0];
#my $word=$ARGV[1];
my $base=0;
my $gcg_count=0;
my $cg_count=0;
my $gch_count=0;
my $hcg_count=0;
my @data_tmp=('n','n','n','n');
open(FH,"<$file_name") or die;
while(<FH>){
	my $line = $_;
	chomp($line);
	if($line=~/chr\S+/){
		next;
	}
	else{
		
		my @splitline=split "",$line;
		$data_tmp[2] = $splitline[0];
		$data_tmp[3] = $splitline[1];
		&check(@data_tmp);
		&check(@splitline);
		$data_tmp[0] = $splitline[$#splitline-1];
		$data_tmp[1] = $splitline[$#splitline];

	}

}
$base *=2;
$cg_count *=2;
print "base: $base\n";
print "cg: $cg_count\n";
print "gcg: $gcg_count\n";
print "gch: $gch_count\n";
print "hcg: $hcg_count\n";

sub check(){
	my @splitline = @_;
	
	
	for(my $i=0;$i<=$#splitline-2;$i++){
		if($splitline[$i] eq 'n' or $splitline[$i] eq 'N'){
			next;
		}
		$base++;
		if(($splitline[$i+2] eq 'G' or $splitline[$i+2] eq 'g') and ($splitline[$i+1] eq 'C' or $splitline[$i+1] eq 'c')){ #forward
			if($splitline[$i] eq 'G' or $splitline[$i] eq 'g'){
				$gcg_count++;
			}
			else{
				$hcg_count++;
			}
		}
		if(($splitline[$i+2] eq 'C' or $splitline[$i+2] eq 'c') and ($splitline[$i+1] eq 'G' or $splitline[$i+1] eq 'g')){ #reverse
			if($splitline[$i] eq 'C' or $splitline[$i] eq 'c'){
				$gcg_count++;
			}
			else{
				$gch_count++;
			}
		}
		if(($splitline[$i] ne 'C' or $splitline[$i] ne 'c') and ($splitline[$i+1] eq 'G' or $splitline[$i+1] eq 'g')){ #reverse
			if($splitline[$i+2] eq 'C' or $splitline[$i+2] eq 'c'){
				$hcg_count++;
			}

		}
		if(($splitline[$i] eq 'G' or $splitline[$i] eq 'g') and ($splitline[$i+1] eq 'C' or $splitline[$i+1] eq 'c')){ #forward
			if($splitline[$i+2] ne 'G' or $splitline[$i+2] ne 'g'){
					$gch_count++;
			}

		}
		if(($splitline[$i] eq 'C' or $splitline[$i] eq 'c') and ($splitline[$i+1] eq 'G' or $splitline[$i+1] eq 'g')){
			$cg_count++;
		}

	}
}
